using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using DTO;
using System.Configuration;
using System.Data;
namespace DAL
{
    public class KHUVUC_DAL
    {
        
        private SqlConnection cn = null;
        public KHUVUC_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"]
                 .ConnectionString);
            cn.Open();
          
        }
        public void insert(KHUVUC_DTO khuvuc)
        {
            SqlCommand cmd = new SqlCommand("KHUVUC_INSERT", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@TENKHUVUC",khuvuc.TenKhuVuc);
            cmd.ExecuteNonQuery();
        }
        public void delete(int makhuvuc)
        {
            SqlCommand cmd = new SqlCommand("KHUVUC_DELETE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MAKHUVUC",makhuvuc);
            cmd.ExecuteNonQuery();
        }
        public void update(KHUVUC_DTO khuvuc)
        {
            SqlCommand cmd = new SqlCommand("KHUVUC_UPDATE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MAKHUVUC", khuvuc.MaKhuVuc);
            cmd.Parameters.AddWithValue("@TENKHUVUC",khuvuc.TenKhuVuc);
            cmd.ExecuteNonQuery();
        }
        public tbKhuVucCollection Get(String where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("KHUVUC_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@WHERE", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PAGESIZE", pageSize);
            cm.Parameters.AddWithValue("@ORDERBY", orderBy);
            cm.Parameters.AddWithValue("@ORDERDIRECTION", orderDirection);

            //cm.Parameters .Add (new SqlParameter ("@TOTALRECORD",DbType.Int32)).Direction=ParameterDirection.Output 
            cm.Parameters.Add(new SqlParameter("@TOTALRECORD", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbKhuVucCollection khuvucCollection = new tbKhuVucCollection();
            while (dr.Read())
            {
                KHUVUC_DTO khuvuc = new KHUVUC_DTO();
                khuvuc.MaKhuVuc = int.Parse(dr.GetValue(0).ToString());
                khuvuc.TenKhuVuc = dr.GetValue(1).ToString();
                khuvucCollection.Add(khuvuc);


            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TOTALRECORD"].Value.ToString());
            return khuvucCollection;


        }
    }
}
